//
//  YDYTOPWindow.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 2016/8/13.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYTOPWindow.h"

@implementation YDYTOPWindow
//window没有强引用，防止挂了
static UIWindow *window_;
+(void)show{
    //延迟0.25秒执行，防止启动的时候崩溃
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        window_ = [[UIWindow alloc]init];
        
        window_.frame = [UIApplication sharedApplication].statusBarFrame;
        //默认黑色
        window_.backgroundColor = [UIColor clearColor];
        //设置为最高级别
        window_.windowLevel = UIWindowLevelAlert;
        window_.hidden = NO;
        
        [window_ addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TopWindowDidClick)]];
    });
 
}
+(void)TopWindowDidClick{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [self findScrollViewInView:window];
   
}

+(void)findScrollViewInView:(UIView *)view{
   //利用递归让所有控件都来这里
    for (UIView *subview in view.subviews) {
        [self findScrollViewInView:subview];
    }
    //拿到scrollview
    if (![view isKindOfClass:[UIScrollView class]])return;
    //判断当前的scrollview是不是在显示（是不是和window重叠）
    if (![view ydy_intersectWithView:[UIApplication sharedApplication].keyWindow])return;
    
    UIScrollView *scrollview  = (UIScrollView *)view;
    CGPoint offset = scrollview.contentOffset;
    offset.y = - scrollview.contentInset.top;
    [scrollview setContentOffset:offset animated:YES];
    
    
    
}
@end
